package com.itheima.homework.test01;

public class TicketSeller1 extends Thread {
    // 假设有100张票
    private static int tickets = 100;

    public TicketSeller1() {
    }


    public TicketSeller1(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true) {
            // 模拟卖票过程
            if(sell()){
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static synchronized boolean sell(){
        if (tickets <= 0) {
            return true;
        } else {
            tickets--;
            System.out.println(Thread.currentThread().getName() + " 卖出了1张票，剩余票数: " + tickets);
            return false;
        }
    }


    public static void main(String[] args) {
        // 创建三个卖票窗口
        TicketSeller1 window1 = new TicketSeller1("窗口1");
        TicketSeller1 window2 = new TicketSeller1("窗口2");
        TicketSeller1 window3 = new TicketSeller1("窗口3");


        // 启动三个窗口的卖票线程
        window1.start();
        window2.start();
        window3.start();
    }
}
